package com.cskaoyan.mall.dao.admin;

import com.cskaoyan.mall.model.User;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao{
    @Override
    public List<User> allUser() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> users = null;
        try {
            users = runner.query("select * from user",new BeanListHandler<>(User.class));

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    @Override
    public List<User> searchUser(String word) {
        String basesql = "select * from user where nickname like ? ";
        String param = "%" + word + "%";
        System.out.println(basesql);
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> users = null;
        try {
            users = runner.query(basesql,new BeanListHandler<>(User.class),
                    param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    /**
     * 删除指定id的用户数据
     * @param id
     * @return
     */
    @Override
    public int deleteUser(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from user where id = ? ",id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }
}
